package com.z.redis.command.impl.list;

import com.z.redis.RedisCore;
import com.z.redis.command.Command;
import com.z.redis.command.CommandType;
import com.z.redis.command.WriteCommand;
import com.z.redis.datatype.BytesWrapper;
import com.z.redis.datatype.RedisList;
import com.z.redis.resp.BulkString;
import com.z.redis.resp.Resp;
import io.netty.channel.ChannelHandlerContext;

public class Rpop implements WriteCommand {
    private BytesWrapper key;

    @Override
    public CommandType type(){
        return CommandType.rpop;
    }

    @Override
    public void setContent(Resp[] array){
        key = ((BulkString) array[1]).getContent();
    }

    @Override
    public void handle(ChannelHandlerContext ctx, RedisCore redisCore){
        RedisList redisList = (RedisList) redisCore.get(key);
        BytesWrapper last = redisList.rpop();
        ctx.writeAndFlush(new BulkString(last));
    }

    @Override
    public void handle(RedisCore redisCore){
        RedisList redisList = (RedisList) redisCore.get(key);
        BytesWrapper last = redisList.rpop();
    }
}
